// 定义db
const db = wx.cloud.database()
// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 获取评论内容
    Comments: [],
    foods: [],
    activities: []
  },
  // 跳转到店铺页面
  toStore(e){
    let storeName = e.currentTarget.dataset.item.storeName
    wx.navigateTo({
      url: '/pages/stores/stores?storeName='+storeName
    })
  },
  // 内容点击事件
  clickComments(e) {
    wx.navigateTo({
      url: `/pages/commentDetails/commentDetails?id=${this.data.Comments[e.currentTarget.id]._id}&userTelphone=${this.data.Comments[e.currentTarget.id].userTelphone}&username=${this.data.Comments[e.currentTarget.id].username}&headImage=${this.data.Comments[e.currentTarget.id].headImage}`
    })
  },
  // 获取图片
  getCommentsImg() {
    let nowArr = []
    for (let i in this.data.Comments) {
      if(this.data.Comments[i].imgPathID.length>0){
        nowArr.push(this.data.Comments[i])
      }
      for(let j in nowArr){
        wx.cloud.downloadFile({
          fileID: nowArr[j].imgPathID[0],
          success: (res) => {
            nowArr[i].commentsSrc = res.tempFilePath
          }
        })
      }
    }
    setTimeout(() => {
      for(let i of this.data.Comments){
        if(i.commentsSrc == undefined){
          // 再次获取图片
          this.getCommentsImg()
        }
      }
      this.setData({
        Comments: nowArr
      })

    }, 1000)

  },
  // 获取点评内容
  getComments() {
    const _that = this
    var nowArr = []
    let userTel = []
    // 循环遍历获取发布人的手机号码
    db.collection('CR_user').get({
      success: (res) => {
        for (let i of res.data) {
          db.collection('CR_comments').where({
            userTelphone: i.telphone
          }).get({
            success: (res) => {
              if (res.data.length > 0) {
                userTel.push(i.telphone)
              }
            }
          })
        }
        setTimeout(() => {
          for (let i of userTel) {
            db.collection('CR_comments').where({
              userTelphone: i
            }).get({
              success: (res) => {
                if (res.data.length > 0) {
                  for (let j in res.data) {
                    nowArr.push(res.data[j])
                  }
                }
              }
            })
          }
          setTimeout(() => {
            this.setData({
              Comments: nowArr
            })
            //获取用户信息
            this.getuser()
            // 获取图片
            this.getCommentsImg()
          }, 500)

        }, 500)
      }
    })
  },
  //获取用户信息
  getuser(){
   let user = this.data.Comments
    for(let i in this.data.Comments){
    db.collection('CR_user').where({
      telphone:this.data.Comments[i].userTelphone
    }).get({
      success:(e)=>{
        user[i].headImage=e.data[0].headImage
        user[i].username=e.data[0].username
      }
    })
  }
  setTimeout(()=>{
          // 清除加载中
          wx.hideLoading()
// this.setData({
//   Comments:user
// })
  },500)
  },

  todetails(e) {
    let foodName = e.currentTarget.dataset.item.foodName
    wx.navigateTo({
      url: '/pages/details/details?foodName=' + foodName,
      success: (result) => {}
    })
  },

  // 获取轮播图数据
  yunGetGoodStores() {
    //云数据根据食物类型查询
    let that = this
    db.collection('CR_stores')
    .where({varyety:'正餐优选'})
      .get({
        success: (res) => {
          that.setData({
            activities: res.data
          })
        }
      })
  },

  yunGet() {
    //云数据根据食物类型查询
    let that = this
    db.collection('CR_foods')
      .where({recommend:'1'})
      .get({
        success: (res) => {
          that.setData({
            foods: res.data
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 显示加载状态
    this.yunGet()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
        // 获取点评内容
        this.getComments()
        // 获取轮播图
        this.yunGetGoodStores()
        // 获取食物数据
        this.yunGet()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // 清除加载中
    wx.hideLoading({
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getComments()
    wx.showToast({
      title: '刷新成功！',
      icon: 'none'
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})